package ui.listeners;

import java.io.File;

import org.apache.pivot.collections.Sequence;
import org.apache.pivot.util.Vote;
import org.apache.pivot.wtk.FileBrowserSheet;
import org.apache.pivot.wtk.Prompt;
import org.apache.pivot.wtk.Sheet;
import org.apache.pivot.wtk.SheetCloseListener;
import org.apache.pivot.wtk.TabPane;
import org.apache.pivot.wtk.TabPaneListener;
import org.apache.pivot.wtk.content.ButtonData;

import ui.other.TabData;
import ui.window.MDKWindow;

public class MDKTabPaneListener extends TabPaneListener.Adapter{
	
	private Prompt confirmCloseTabPrompt;
	private Boolean confirmCloseTab = true;
	private MDKWindow appli;
	
	public MDKTabPaneListener(MDKWindow appli, Prompt prompt){
		this.appli= appli;
		confirmCloseTabPrompt = prompt;
	}
	
	
	public Vote previewRemoveTabs(final TabPane tabPane, final int index, final int count) {
        if(confirmCloseTab){
            confirmCloseTabPrompt.open(appli.getWindow(), new SheetCloseListener() {
            	
            	public void sheetClosed(Sheet sheet) {
            		if (confirmCloseTabPrompt.getResult()){
            			if(confirmCloseTabPrompt.getSelectedOptionIndex() == 0) {
            				ButtonData onglet = (ButtonData) TabPane.getTabData(tabPane.getTabs().get(index));
            				TabData tabData = ((TabData)onglet.getUserData());
            				
            				if(tabData!=null && tabData.getPathToFile() != null)
            				{
            					appli.saveFileAs(new File(tabData.getPathToFile()));
            					
            					int n = tabPane.getTabs().getLength();
            					
                                if(n==1)
                                	appli.addTab("New file*", null);
                                else
                                {
	                                if (index < n - 1) {
	                                    tabPane.setSelectedIndex(index + 1);
	                                } else {
	                                    tabPane.setSelectedIndex(index - 1);
	                                }
                                }
                                
                                confirmCloseTab = false;
                                tabPane.getTabs().remove(index, count);
                                confirmCloseTab = true;
            				}
            				else
            				{
            					FileBrowserSheet.Mode fileBrowserSheetMode = FileBrowserSheet.Mode.valueOf("SAVE_AS");
            	            	final FileBrowserSheet fileBrowserSheet = new FileBrowserSheet();

            	            	fileBrowserSheet.setMode(fileBrowserSheetMode);
            	            	fileBrowserSheet.open(appli.getWindow(), new SheetCloseListener() {
            	            		
            	            		public void sheetClosed(Sheet sheet) {
            	            			if (sheet.getResult()) {
            	            				Sequence<File> selectedFiles = fileBrowserSheet.getSelectedFiles();
            	            				appli.saveFileAs(selectedFiles.get(0));
            	            				
            	            				int n = tabPane.getTabs().getLength();
    	                					
        	                                if(n==1)
        	                                	appli.addTab("New file*", null);
        	                                else
        	                                {
    	    	                                if (index < n - 1) {
    	    	                                    tabPane.setSelectedIndex(index + 1);
    	    	                                } else {
    	    	                                    tabPane.setSelectedIndex(index - 1);
    	    	                                }
        	                                }
        	                                
        	                                confirmCloseTab = false;
        	                                tabPane.getTabs().remove(index, count);
        	                                confirmCloseTab = true;
            	            			}
            	            		}
            	            	});
            				}
                        }
            			
            			
            			if(confirmCloseTabPrompt.getSelectedOptionIndex() == 1) {
            				 
            				int n = tabPane.getTabs().getLength();
        					
                            if(n==1)
                            {
                            	appli.addTab("New file*", null);
                            	tabPane.setSelectedIndex(1);
                            }
                            else
                            {
                                if (index < n - 1) {
                                    tabPane.setSelectedIndex(index + 1);
                                } else {
                                    tabPane.setSelectedIndex(index - 1);
                                }
                            }
                            
                            confirmCloseTab = false;
                            tabPane.getTabs().remove(index, count);
                            confirmCloseTab = true;
                        }
            		}
                }
           });

            return Vote.DENY;
        }
        else
        	return Vote.APPROVE;
    }
}
